package com.liuzhilin.service;

import com.liuzhilin.domain.Message;
import com.liuzhilin.domain.User;
import com.liuzhilin.mapper.DatabaseOperations;
import com.liuzhilin.mapper.DatabaseSQl;
import jakarta.servlet.http.HttpServletResponse;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class MessageService {
    //获取消息
    public static List<Message> getMessage(String username,int type){
        username=username.trim();
        //创建一个List集合
        List<Message> messageList = new ArrayList<>();
        //查询发出去的信息
        if (type==10){
            Object[] objects = {username};
            try {
                ResultSet resultSet = DatabaseOperations.executeQuery(DatabaseSQl.selectMessageByUserSql(), objects);
                while (resultSet.next()){
                    Message message = new Message(resultSet.getString("sendMessage"),resultSet.getString("receiveMessage"),resultSet.getString("content"),resultSet.getInt("type"),resultSet.getString("time"));
                    if (message.getType()==1 || message.getType()==0){
                        messageList.add(message);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }else{
            //查询收到的信息
            Object[] objects = {username,type};
            try {
                ResultSet resultSet = DatabaseOperations.executeQuery(DatabaseSQl.selectMessageSql(), objects);
                while (resultSet.next()){
                    Message message = new Message(resultSet.getString("sendMessage"),resultSet.getString("receiveMessage"),resultSet.getString("content"),resultSet.getInt("type"),resultSet.getString("time"));
                    messageList.add(message);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return messageList;
    }
    //修改消息状态
    public static boolean updateMessageType(String username,String message){
        try {
            DatabaseOperations.executeUpdate(DatabaseSQl.updateMessageTypeSql(),new Object[]{username,message});
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    //获取搜索的用户信息
    public static List<User> getUsers(String type, String content){
        // 构建SQL查询语句
        String sql;
        List<User> users = new ArrayList<>();
        //  根据用户输入的搜索类型和内容构建SQL查询语句
        if ("id".equals(type)) {
            sql =DatabaseSQl.selectUserByIdSql();
            int id= Integer.parseInt(content);
            Object[] objects = {id};
            try {
                ResultSet resultSet = DatabaseOperations.executeQuery(sql, objects);
                while (resultSet.next()) {
                    User user = new User(resultSet.getInt("id"), resultSet.getString("userName"), resultSet.getString("password"), resultSet.getString("personalSignature"), resultSet.getInt("articleCount"), resultSet.getDate("birthday"), resultSet.getInt("status"));
                    users.add(user);
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        } else if ("username".equals(type)) {
            sql = DatabaseSQl.userNameLikeSql();
            Object[] objects = {content};
            try {
                ResultSet resultSet = DatabaseOperations.executeQuery(sql, objects);
                while (resultSet.next()) {
                    User user = new User(resultSet.getInt("id"), resultSet.getString("userName"), resultSet.getString("password"), resultSet.getString("personalSignature"), resultSet.getInt("articleCount"), resultSet.getDate("birthday"), resultSet.getInt("status"));
                    users.add(user);
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        } else {
            return null;
        }
        return users;
    }
    //添加好友
    public static boolean addFriend(String username,String friendName){
        //查询是否已经时好友了
        Object[] objects = {username,friendName};
        try {
            ResultSet resultSet = DatabaseOperations.executeQuery(DatabaseSQl.selectFriendSql(), objects);
            if (resultSet.next()){
                return false;
            }else {
                DatabaseOperations.executeUpdate(DatabaseSQl.addFriendSql(),objects);
                sendMessage(username,"用户:"+username+"添加了您为好友",friendName,0);
                DatabaseOperations.executeUpdate(DatabaseSQl.addFriendSql(),new Object[]{friendName,username});
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    //获取好友列表
    public static List<String> getFriends(String username){
        username=username.trim();
        List<String> friends = new ArrayList<>();
        try {
            ResultSet resultSet = DatabaseOperations.executeQuery(DatabaseSQl.selectFriendByNameSql(), new Object[]{username});
            while (resultSet.next()){
                //记录好友的名称
                String friendName = resultSet.getString("secondFriend");
                friends.add(friendName);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return friends;
    }
    //发送信息
    public static boolean sendMessage(String username,String message,String friendName,int type){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        username=username.trim();
        friendName=friendName.trim();
        try {
            DatabaseOperations.executeUpdate(DatabaseSQl.recordOperationLogSql(),new Object[]{username,friendName,message,type,formattedDateTime});
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    //查询历史聊天记录
    public static List<Message> getHistoricalMessages(String username,String friendName) {
        List<Message> messageList = new ArrayList<>();
        username=username.trim();
        friendName=friendName.trim();
        try {
            ResultSet resultSet = DatabaseOperations.executeQuery(DatabaseSQl.getHistoricalMessagesSql(), new Object[]{username,friendName,friendName,username});
            while (resultSet.next()) {
                Message message = new Message(resultSet.getString("sendMessage"), resultSet.getString("receiveMessage"), resultSet.getString("content"), resultSet.getInt("type"), resultSet.getString("time"));
                messageList.add(message);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return messageList;


    }
}
